/**
 * 
 */
package Verifier.ArithmeticSystem;

import java.util.ArrayList;
import java.util.List;

/**
 * This class represents a product field element which contains a number of elements inside it, 
 * which may be one of the following types: FieldElement or ProductFieldElement.
 * 
 * @author Moshe Baavur
 *
 */
public class ProductFieldElement extends Element {
	// list to keep all the elements in the product
	List<Element> elements;
	
	/**
	 * Creates a new empty product field ring
	 */
	public ProductFieldElement()	{
		// initialize the elements list
		elements = new ArrayList<Element>();
	}
	
	/**
	 * Adds a field element into the field ring at the last index.
	 * 
	 * @param element the field element to add.
	 */
	public void addFieldElement(FieldElement element) {
		elements.add(element);
	}
	
	/**
	 * Adds a product field ring element into the product field ring at the last index.
	 * 
	 * @param productElement the product ring element to add.
	 */
	public void addProductFieldElement(ProductFieldElement productElement) {
		elements.add(productElement);
	}
	
	/**
	 * Returns the element in the product field ring which has index which is equal to the passed parameter.
	 * 
	 * @param index the index of the object to return.
	 * @return the element at the specified index in the ring.
	 * @throws IndexOutOfBoundsException - if index is out of the ranges of the ring - (index < 0 || index >= getNumberOfElements()).
	 */
	public Element getElement(int index) throws IndexOutOfBoundsException {
		return (elements.get(index));
	}
	
	/**
	 * Returns the number of elements in the product field ring.
	 * 
	 * @return the number of elements in the product field ring.
	 */
	public int getNumberOfElements() {
		return (elements.size());
	}
	
	/**
	 * Compare this to obj. They are equal only if obj is of type FieldElement and their field and element are equal.
	 * 
	 * @param obj the object to compare to this.
	 * @return true if and only if obj is of type FieldElement and the field and the element are equal to this.
	 */
	@Override
	public boolean equals(Object obj) {
		// if the object is a ModularGroupElement, then compare them using their element and fields and not the address
		if (obj instanceof ProductFieldElement) {
			ProductFieldElement objElement = (ProductFieldElement)obj;
			int length = elements.size();
			if (objElement.elements.size() != length) {
				return (false);
			}
			
			for (int i = 0; i < length; i++) {
				// check if the rings have the same elements inside them
				if (!objElement.elements.get(i).equals(elements.get(i))) {
					return (false);
				}
			}
			
			return (true);
		}
		// otherwise, they are not equal
		return (false);
	}
}
